package 打点计时器.src.main;
import java.util.*;
public class Main {
    public static int solution(int[][] inputArray) {

                TreeMap<Integer, Integer> intervals = new TreeMap<>();

                // 将所有区间加入TreeMap
                for (int[] range : inputArray) {
                    intervals.put(range[0], intervals.getOrDefault(range[0], 0));
                    intervals.put(range[1] + 1, intervals.getOrDefault(range[1] + 1, 0) - 1);
                }

                int count = 0;
                int activeIntervals = 0;

                // 遍历TreeMap，合并区间并计算打点数
                for (Map.Entry<Integer, Integer> entry : intervals.entrySet()) {
                    activeIntervals += entry.getValue();
                    if (activeIntervals > 0) {
                        count++;
                    }
                }

                return count;
            }



    public static void main(String[] args) {
        //  You can add more test cases here
        int[][] testArray1 = {{1, 4}, {7, 10}, {3, 5}};
        int[][] testArray2 = {{1, 2}, {6, 10}, {11, 15}};

        System.out.println(solution(testArray1) == 7);
        System.out.println(solution(testArray2));
        System.out.println(solution(testArray2) == 9);
    }
}